#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define endl '\n'
const int N = 3e5 + 5;

int n, m;
int a[N], cnt[N];
int head[N];
int ver[N << 1], Next[N << 1], tot;
void add(int u, int v) {
  ++tot;
  ver[tot] = v;
  Next[tot] = head[u];
  head[u] = tot;
}
int dep[N], sz[N], fa[N], son[N], top[N];
int pos[N], num;
void dfs(int u, int fu) {
  dep[u] = dep[fu] + 1;
  sz[u] = 1;
  fa[u] = fu;
  for (int i = head[u]; i; i = Next[i]) {
    int v = ver[i];
    if (v == fu) continue;
    dfs(v, u);
    sz[u] += sz[v];
    if (sz[v] > sz[son[u]]) son[u] = v;
  }
}
void dfs2(int u, int fu, int t) {
  top[u] = t;
  pos[u] = ++num;
  if (!son[u]) return;
  dfs2(son[u], u, t);
  for (int i = head[u]; i; i = Next[i]) {
    int v = ver[i];
    if (v == fu || v == son[u]) continue;
    dfs2(v, u, v);
  }
}
int bit[N];
int lowbit(int x) { return x & -x; }
void update(int x, int d) {
  for (int i = x; i <= n; i += lowbit(i)) bit[i] += d;
}
int query(int x) {
  int res = 0;
  for (int i = x; i > 0; i -= lowbit(i)) res += bit[i];
  return res;
}
void update(int u, int v, int d) {
  while (top[u] != top[v]) {
    if (dep[top[u]] < dep[top[v]]) swap(u, v);
    update(pos[top[u]], d);
    update(pos[u] + 1, -d);
    u = fa[top[u]];
  }
  if (dep[u] > dep[v]) swap(u, v);
  update(pos[u], d);
  update(pos[v] + 1, -d);
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n;
  rep(i, 1, n) cin >> a[i], cnt[a[i]]++;
  rep(i, 2, n) {
    int u, v;
    cin >> u >> v;
    add(u, v), add(v, u);
  }
  dfs(1, 0);
  dfs2(1, 0, 1);
  rep(i, 1, n) {
    if (i == 1)
      update(a[1], a[1], 1);
    else
      update(a[i - 1], a[i], 1);
  }
  rep(i, 1, n) cout << query(pos[i]) - cnt[i] << endl;
  return 0;
}